package com.example.demo.controller;

import com.example.demo.pojo.Catrgoryicon;
import com.example.demo.pojo.Order;
import com.example.demo.pojo.OrderParamaccepttijiao;
import com.example.demo.service.OrdersBackService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author yanglup
 * @create 2020-11-30 19:21
 */
@Controller
public class BackOrdersController {
    @Autowired
    OrdersBackService ordersBackService;

    @RequestMapping("/Orderslist")
    public String BackOrderslist(Model model, HttpServletRequest request) {
        Integer pageNo=request.getParameter("pageNo")==null?1:Integer.valueOf(request.getParameter("pageNo"));
        PageInfo<Order> pageInfo=ordersBackService.BackOrderslist(pageNo,8);

        List<Order> list=pageInfo.getList();
        model.addAttribute("myorderslists",list);
        model.addAttribute("pages",pageInfo.getPages());
        model.addAttribute("pageNo",pageInfo.getPageNum());
        model.addAttribute("pageSize",pageInfo.getPageSize());

        Integer allnumber= ordersBackService.Ordersallnumber();
        model.addAttribute("allnumber",allnumber);
        return "Orderslist";
    }

//    /**查询全部订单*/
//    @RequestMapping("/Orderslist")
//    public String BackOrderslist(Model model){
//        List<Order> list= ordersBackService.BackOrderslist();
//        model.addAttribute("myorderslists",list);
//        Integer allnumber= ordersBackService.Ordersallnumber();
//        model.addAttribute("allnumber",allnumber);
////        System.out.println(list);
//        return "Orderslist";
//    }


    /**订单添加1--跳转到add*/
    @RequestMapping("/Ordersadd")
    public String Ordersadd(Model model){
        return "Ordersadd";
    }
    /**订单提交2--保存数据跳回list*/
    @PostMapping("/Ordersaddtijiao")//post
    public String Ordersaddtijiao(Model model, OrderParamaccepttijiao orderParamaccepttijiao){
//        System.out.println(orderParamaccepttijiao);
        //提交到数据库
        String openid=orderParamaccepttijiao.getOpenid();
        String openidbuy=orderParamaccepttijiao.getOpenidbuy();
        String consignee_addr=orderParamaccepttijiao.getConsignee_addr();
        Integer order_price=orderParamaccepttijiao.getOrder_price();
        Integer goods_id=orderParamaccepttijiao.getGoods_id();
        Integer goods_number=orderParamaccepttijiao.getGoods_number();
        Integer goods_price=orderParamaccepttijiao.getGoods_price();
        Integer order_status=orderParamaccepttijiao.getOrder_status();
        String goods_name=orderParamaccepttijiao.getGoods_name();
        String order_number=System.currentTimeMillis()+"123";//创建订单时候需要系统的时间戳    因为需要创建图书里查询订单编号，但直接创建订单不需要图书就我们自己写一个---基本上不需要创建订单
        Integer buyer=orderParamaccepttijiao.getBuyer();
        Integer seller=orderParamaccepttijiao.getSeller();
        String goods_small_logo=orderParamaccepttijiao.getGoods_small_logo();
        ordersBackService.createorderstableback(openid,openidbuy,consignee_addr,order_price,goods_id,goods_number,goods_price,order_status,goods_name,order_number,buyer,seller,goods_small_logo);
        return "redirect:/Orderslist";
    }



//    @RequestMapping("/Ordersupdate/{goods_id}")
//    public String Ordersupdate(Model model,@PathVariable("goods_id")Integer goods_id){
//        System.out.println(goods_id);
//        return "Ordersupdate";
//    }
    /**跳转到修改页面*/
    @RequestMapping("/Ordersupdate")
    public String Ordersupdate(Model model, Integer goods_id){

        //通过d查询商品信息
        System.out.println(goods_id);
        OrderParamaccepttijiao orderParamaccepttijiao= ordersBackService.selectorderParamaccepttijiao(goods_id);
        model.addAttribute("orderParamaccepttijiao",orderParamaccepttijiao);
        System.out.println(orderParamaccepttijiao);
        return "Ordersupdate";
    }
    /**订单提交2--保存数据跳回list------唯一没有修改goods_id 具体要看商品添加表的goods_id   这里不建议修改*/
    @PostMapping("/Ordersupdatetijiao")//post
    public String Ordersupdatetijiao(Model model, OrderParamaccepttijiao orderParamaccepttijiao){
        System.out.println(orderParamaccepttijiao);
        //提交到数据库
        Integer order_id=orderParamaccepttijiao.getOrder_id();  //是修改后提交时
        String openid=orderParamaccepttijiao.getOpenid();
        String openidbuy=orderParamaccepttijiao.getOpenidbuy();
        String consignee_addr=orderParamaccepttijiao.getConsignee_addr();
        Integer order_price=orderParamaccepttijiao.getOrder_price();
        Integer goods_id=orderParamaccepttijiao.getGoods_id();
        Integer goods_number=orderParamaccepttijiao.getGoods_number();
        Integer goods_price=orderParamaccepttijiao.getGoods_price();
        Integer order_status=orderParamaccepttijiao.getOrder_status();
        String goods_name=orderParamaccepttijiao.getGoods_name();
        String order_number=orderParamaccepttijiao.getOrder_number();
        Integer buyer=orderParamaccepttijiao.getBuyer();
        Integer seller=orderParamaccepttijiao.getSeller();
        String goods_small_logo=orderParamaccepttijiao.getGoods_small_logo();

        ordersBackService.updateorderstableback(order_id,openid,openidbuy,consignee_addr,order_price,goods_id,goods_number,goods_price,order_status,goods_name,order_number,buyer,seller,goods_small_logo);
        return "redirect:/Orderslist";
    }



    /**删除商品*/
    @RequestMapping("/Ordersdelete")
    public String Ordersdelete(Integer goods_id){
        ordersBackService.Ordersdelete(goods_id);
        return "redirect:/Orderslist";
    }

    /**通过id搜索商品*/
    @RequestMapping("/Ordersearch")
    public String Ordersearch(String goods_name,Model model){
        List<Order> ordersearchlist= ordersBackService.Ordersearch(goods_name);
        model.addAttribute("ordersearchlists",ordersearchlist);
        Integer allnumber= ordersBackService.Ordersearchnumber(goods_name);
        model.addAttribute("allnumber",allnumber);
        return "Orderslistquery";
    }



}
